package com.cssw.fyzb.web.service;

import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.core.dao.live.AnchorDao;
import com.cssw.fyzb.core.dao.live.LiveDao;
import com.cssw.fyzb.core.dao.live.LiveReportDao;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.dao.video.UserVideoDao;
import com.cssw.fyzb.core.dao.video.VideoReportDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.web.vo.LiveReportVo;
import com.cssw.fyzb.web.vo.VideoReportVo;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class VideoReportService {
    @Autowired
    private VideoReportDao videoReportDao;

    @Autowired
    private UserVideoDao userVideoDao;

    @Autowired
    private UserDao userDao;
    @Autowired
    private AnchorDao anchorDao;
    /**
     * 查询菜单列表
     *
     * @param page
     * @param limit
     * @param param
     * @return
     */
    public AjaxPage list(int page, int limit, Map param) {

        Page<CmfVideoReport> pages = videoReportDao.list(page, limit, param);
        List<VideoReportVo> videoReportVos = new ArrayList<>();
        if (null != pages.getList() && pages.getList().size() != 0) {
            VideoReportVo videoReVo = null;
            for (CmfVideoReport live: pages.getList()) {
                CmfUserVideo cmfUserVideo = userVideoDao.findById(live.getVideoId());
                User    userFrom = userDao.getById(live.getFromUserId());
                User    userTo   = userDao.getById(live.getToUserId());
                videoReVo= new VideoReportVo();
                videoReVo.setReportId(live.getReportId());
                videoReVo.setVideoId(cmfUserVideo.getId());
                videoReVo.setFromUserId(live.getFromUserId());
                videoReVo.setToUserId(live.getToUserId());
                videoReVo.setContent(live.getContent());
                videoReVo.setStatus(live.getStatus());
                videoReVo.setCreateTime(live.getCreateTime());
                videoReVo.setFromUserName(userFrom.getUserName());
                videoReVo.setToUserName(userTo.getUserName());
                videoReVo.setTitle(cmfUserVideo.getTitle());
                videoReVo.setHref(cmfUserVideo.getHref());
                videoReVo.setThumb(cmfUserVideo.getThumb());
//                videoReVo.setState(cmfUserVideo.getState());
                videoReportVos.add(videoReVo);
            }
        }
        return AjaxPage.success(videoReportVos, pages.getTotalRow(), pages.getTotalPage());
    }


    public boolean update(Long reportId){
      Boolean flag = Db.tx(() -> {
          CmfVideoReport cmfVideoReport= videoReportDao.findOne(reportId);
          userVideoDao.delete(cmfVideoReport.getVideoId());
          videoReportDao.updateStatus(reportId,"2");
          return true;
        });
        return flag;
    }

    public boolean reject(Long reportId){
        Boolean flag = Db.tx(() -> {
            CmfVideoReport cmfVideoReport= videoReportDao.findOne(reportId);
            userVideoDao.delete(cmfVideoReport.getVideoId());
            videoReportDao.updateStatus(reportId,"3");
            return true;
        });
        return flag;
    }
}
